package GUI;

import java.awt.Rectangle;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import Client_logic.DealSale;
import Client_logic.Template;
import Client_logic.ConnectionToServer;

/**
 * This class use to search for active Campaignh
 * @author michael
 * @author ido
 * @author michael
 * @author matan
 *
 */
public class DealSaleSearch extends JFrame
{
	private JComboBox cIDComboBox = null;
	private JLabel cIDlLabel = null;
	private JButton searchButton = null;
	private JPanel jContentPane = null;
	private long cID = 0;
	private ReportForm reportForm = null;
	
	
	/**
	 * This is the default constructor
	 */
	public DealSaleSearch() 
	{
		super();
		initialize();		
	}
	
	/**
	 * This method initializes ProductForm
	 */
	private void initialize() 
	{
		this.setContentPane(getJContentPane());
		this.setTitle("Campaign SEARCH");
	}
	
	private JPanel getJContentPane() 
	{
		if (jContentPane == null) 
		{
			jContentPane = new JPanel();
			jContentPane.setLayout(null);
			
			cIDlLabel = new JLabel();
			cIDlLabel.setBounds(new Rectangle(15, 17, 113, 22));
			cIDlLabel.setText("Campaign number:");
							
			jContentPane.setLayout(null);
			jContentPane.add(cIDlLabel, null);
			jContentPane.add(getCIDComboBox(), null);
			jContentPane.add(getSearchButton(),null);			
		}
		setSize(300, 300);
		return jContentPane;		
	}
	
	/**
	 * This method initializes PIDComboBox	
	 */
	private JComboBox getCIDComboBox()
	{
		if(cIDComboBox == null)
		{	
			cIDComboBox = new JComboBox();
			cIDComboBox.setBounds(new Rectangle(133, 17, 127, 25));
			long[] campaignsID = null;
			try
			{
				ConnectionToServer connectiontoserver = new ConnectionToServer();		
				campaignsID = (long[]) connectiontoserver.loadAllDealSales();
				if (campaignsID == null)
				{
					JOptionPane.showMessageDialog(null, "Campaigns is EMPTY!!!");
					cIDComboBox.addItem(0);
				}	
				else
				{
					int len = campaignsID.length;
					if (len > 0)
						cID = campaignsID[0];
					for(int i=0; i < len; i++)
					{
						cIDComboBox.addItem(campaignsID[i]);
					}	
				}
			}
			catch(Exception e)
			{
				cIDComboBox.addItem(0);
			}
			cIDComboBox.addActionListener(new java.awt.event.ActionListener() 
			{
				public void actionPerformed(java.awt.event.ActionEvent e) 
				{
					cID = (Long)cIDComboBox.getSelectedItem();	
				}	
			});	
		}	
		return cIDComboBox;
	}

	
	/**
	 * This method initializes searchButton	
	 */
	private JButton getSearchButton() 
	{
		if (searchButton == null) 
		{
			searchButton = new JButton();
			searchButton.setBounds(new Rectangle(90, 70, 130, 29));
			searchButton.setText("Search");
			searchButton.addActionListener(new java.awt.event.ActionListener() 
			{
				public void actionPerformed(java.awt.event.ActionEvent e) 
				{
					try 
					{
						if (cID > 0)
						{
							ConnectionToServer connectionToServer = new ConnectionToServer();
							String msg = (String)connectionToServer.getResponseReport(cID);
							if(msg != null)
							{	
								reportForm = new ReportForm();
								JFrame frame = new JFrame("Reports"); 
								frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
							//Add content to the window. 
								frame.add(reportForm); 
							//Display the window. 
								frame.pack();
								frame.setSize(300, 300);
								reportForm.setText(msg);	
								frame.setVisible(true);													
							}	
							else
								JOptionPane.showMessageDialog(null, "CAMPAIGN NOT EXIST IN DATA BASE","",JOptionPane.WARNING_MESSAGE);
						}
						else
							JOptionPane.showMessageDialog(null, "PLZ select a campaign");
					} catch (Exception excp)
					{
						JOptionPane.showMessageDialog(null, "FATAL ERROR!!!","",JOptionPane.ERROR_MESSAGE);	
					}											
				}	
			});
		}
		return searchButton;
	}
	
	
}
